package com.example.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatEditText;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.appcompat.widget.AppCompatTextView;

import java.util.Objects;

public class WebViewMainActivity extends AppCompatActivity {

    private AppCompatEditText urlEditText;
    private AppCompatImageView sendImageView;
    private WebView mainWebView;
    private AppCompatImageView reloadImageView;
    private AppCompatImageView backImageView;
    private AppCompatImageView forwardImageView;
    private AppCompatImageView homeImageView;
    private AppCompatTextView finishTextView;




    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_wbe);

        urlEditText =findViewById(R.id.urlEditText);
        sendImageView =findViewById(R.id.sendImageView);
        mainWebView =findViewById(R.id.mainWebView);
        reloadImageView =findViewById(R.id.reloadImageView);
        backImageView =findViewById(R.id.backImageView);
        forwardImageView =findViewById(R.id.forwardImageView);
        homeImageView =findViewById(R.id.homeImageView);
        finishTextView = findViewById(R.id.finishTextView);


        //接收父Activity传递的数据
        String url = getIntent().getStringExtra("url");



        //设置webview
        WebSettings webSettings = mainWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        //缓存设置
        webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
        mainWebView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                urlEditText.setText(url);
                mainWebView.loadUrl(url);
                return false;
            }
        });
        mainWebView.loadUrl(url);
        sendImageView.setOnClickListener(view -> {
            mainWebView.loadUrl(Objects.requireNonNull(urlEditText.getText()).toString());

        });

        finishTextView.setOnClickListener(view -> {
            //带结果返回
            Intent intent = new Intent();
            intent.putExtra("url", urlEditText.getText().toString());
            setResult(RESULT_OK, intent);
            finish();
        });

        Log.d("TAG", "WebViewActivity:onCreate");

    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.d("TAG", "WebViewActivity:onStart");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d("TAG", "WebViewActivity:onResume");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d("TAG", "WebViewActivity:onPause");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d("TAG", "WebViewActivity:onStop");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d("TAG", "WebViewActivity:onDestory");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d("TAG", "WebViewActivity:onRestart");

        //刷新
        reloadImageView.setOnClickListener(view -> mainWebView.reload());
        //前进
        forwardImageView.setOnClickListener(view -> mainWebView.goForward());
        //后退
        backImageView.setOnClickListener(view -> mainWebView.goBack());
        //主页
        homeImageView.setOnClickListener(view -> mainWebView.loadUrl("https://v26-web.douyinvod.com/923c6af7a149d95246640ddf1" +
                    "e51cb41/63722ab4/video/tos/cn/tos-cn-ve-15c001-alinc2/554b051462324f48b" +
                    "fed7988d502a8cc/?a=6383&ch=11&cr=3&dr=0&lr=all&cd=0%7C0%7C0%7C3&cv=1&b" +
                    "r=1193&bt=1193&cs=0&ds=3&ft=rVWEerwwZRclse~o5PDS6kFgAX1tGqc14S9eF2CyBq" +
                    "V12ni7t&mime_type=video_mp4&qs=0&rc=" +
                    "%3D%3D&l=2022111418462101015811604413132502&btag=18000"));

    }

}
